package com.fanhao.dllo.soundfm.fragment.Infofragmnet;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.activity.AdverActivity;
import com.fanhao.dllo.soundfm.adapter.FindNewsAdapter;
import com.fanhao.dllo.soundfm.entity.FindNewsResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.tools.MyOnclickListener;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by dllo on 16/1/21.
 */
public class FindNewsFragment extends BaseFragment implements MyOnclickListener, View.OnClickListener {
    private RecyclerView findNewsRv;
    private FindNewsAdapter findNewsAdapter;
    private ImageView backFindNewsIv;
    private String title;
    private TextView titleTv;

    public FindNewsFragment(String title) {
        this.title = title;
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_find_news;
    }

    @Override
    protected void initView() {
        titleTv = findView(R.id.tvTitle_findNews);
        titleTv.setText("倾听FM "+title);
        backFindNewsIv = findView(R.id.ivBack_findNews);
        backFindNewsIv.setOnClickListener(this);
        findNewsRv = findView(R.id.recyclerView_findNews);
        findNewsRv.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        findNewsAdapter = new FindNewsAdapter(getActivity());
        findNewsAdapter.setMyOnclickListener(this);
        findNewsRv.setAdapter(findNewsAdapter);

    }

    @Override
    protected void initData() {
        String url = "http://activity.ximalaya.com/activity-web/activity/activityList?device=android&pageId=1";
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        FindNewsResult result = gson.fromJson(response,
                                FindNewsResult.class);
                        if (result != null) {
                            FindNewsResult.ResultEntity resultEntity = result.getResult();
                            if (resultEntity != null) {
                                List<FindNewsResult.ResultEntity.ActivityDataEntity> activityList =
                                        resultEntity.getActivityData();
                                if (activityList != null && activityList.size() > 0) {
                                    findNewsAdapter.addActivityData(activityList);
                                }
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }

    @Override
    public void myListener(long uid, int pos) {

    }

    @Override
    public void myNoListener(String content,int p) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), AdverActivity.class);
        intent.putExtra("activityId",p);
        getActivity().startActivity(intent);
        getActivity().overridePendingTransition(R.anim.move_in_from_bottom,R.anim.no_move);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack_findNews:
                goBackToRight();
                break;
        }
    }
}
